package com.example.backend.service;

import com.example.backend.entity.Admin;
import com.example.backend.entity.Rider;
import com.example.backend.mapper.RiderMapper;
import com.example.backend.util.PwdUtil;
import com.example.backend.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//骑手服务层 RiderService 的实现
@Service
public class RiderService {
    @Autowired
    private RiderMapper riderMapper;

    public int addRider(Rider rider){
        // 密码加密
        rider.setPwd(PwdUtil.getEncryptedPwd(rider.getPwd()));
        // 设置默认工作状态
        rider.setGzzt("空闲");
        // 设置默认收入和评分
        rider.setGzsr(0.0);
        rider.setMark(5.0);
        // 设置默认认证状态为待认证
        rider.setState("待认证");
        return riderMapper.addRider(rider);
    }

    public int updateRider(Rider rider){
        return riderMapper.updateRider(rider);
    }

    public int deleteRiderById(Serializable id){
        return riderMapper.deleteRiderById(id);
    }

    public Rider selectRiderById(Serializable id){
        return riderMapper.selectRiderById(id);
    }

    public Rider selectRider(Rider rider){
        // 密码加密
        rider.setPwd(PwdUtil.getEncryptedPwd(rider.getPwd()));
        return riderMapper.selectRider(rider);
    }

    public Map<String, Object> selectRiders(Rider rider, int pageNum, int pageSize) {
        // 使用传入的分页参数启动分页
        PageHelper.startPage(pageNum, pageSize);

        // 执行数据库查询 - 这里保持不变
        ArrayList<Rider> riders = riderMapper.selectRiders(rider);

        // 创建PageInfo对象获取分页信息
        PageInfo<Rider> pageInfo = new PageInfo<>(riders);

        // 构建包含数据和分页信息的响应
        Map<String, Object> result = new HashMap<>();
        result.put("list", riders);          // 当前页的数据列表
        result.put("total", pageInfo.getTotal()); // 总记录数
        result.put("pages", pageInfo.getPages()); // 总页数

        return result;
    }

    public int updateRiderScore(Integer id){
        return riderMapper.updateRiderScore(id);
    }
}
